import { deepCopy } from '@/utils'
const mixins = {
  props: {
    acTypeInfo: {
      type: Object,
      default: () => { }
    },
    selectShopList: {
      type: Array,
      default: () => []
    },
    disabled: {
      type: Boolean,
      default: false
    },
    dvyType: {
      type: Number | String,
      default: ''
    }
  },
  watch: {
    selectShopList: function () {
      // 处理用户删除了适用商户的时候 表单数据不更新
      this.form.activityMerchantProdList.forEach((f, index) => {
        if (!this.selectShopList.some(s => s.value == f.activityMerchantId)) {
          this.$set(this.form.activityMerchantProdList[index], 'activityMerchantId', '')
        }
      })
    },
    dvyType: function () {
      this.form.activityMerchantProdList.forEach(f => {
        if (f.activityMerchantId) {
          this.searchProdsByCurrentShop(f, 1)
        }
      })
    }
  },
  data () {
    return {}
  },
  methods: {
    astrictInputE (e) {
      let key = e.key
      if (key === 'e' || key === 'E' || key === '+' || key === '-') {
        e.returnValue = false
        return false
      }
      return true
    },
    inputLimit (e, item, key) {
      let decimals = e.split('.')
      if (decimals[1] && decimals[1].length > 4) {
        e = decimals[0] + '.' + decimals[1].slice(0, 4)
      }
      this.$set(item, key, e)

      if (key == 'discountProportion') {
        if (e > 100) {
          this.$set(item, key, 100)
        } else if (e < 0) {
          this.$set(item, key, 0)
        }
      }
    },
    // 如果是编辑或者查看 需要整理页面数据结构
    collatingEchoData (data) {
      if (data.activityMerchantProdList.length) {
        // 遍历组合
        data.activityMerchantProdList.forEach((f) => {
          f.activityMerchantId = String(f.activityMerchantId)
          // A商品名
          if (f.prodJson) {
            f.prodIds = (JSON.parse(f.prodJson)).prodIds
          }
          // B商品名
          if (f.prodSpecifyJson) {
            f.prodSpecifyIds = (JSON.parse(f.prodSpecifyJson)).prodSpecifyIds
          }
          if (this.acTypeInfo.id == 2 || this.acTypeInfo.id == 7 || this.acTypeInfo.id == 11) {
            if (f.prodSpecifyIds) {
              f.prodSpecifyIds = f.prodSpecifyIds[0]
            }
          }
          // 折扣数值
          if (f.discountProportion !== null && f.discountProportion !== undefined) {
            f.discountProportion = f.discountProportion * 100
          }

          if (this.acTypeInfo.id == 10) {
            f.discountRate = f.discountRate * 100
            f.specifyDiscountRate = f.specifyDiscountRate * 100
          }
          this.searchProdsByCurrentShop(f)
        })
        // 整理好的 子组件的组合列表 给予子组件
        this.form.activityMerchantProdList = data.activityMerchantProdList
        console.log('this.form', this.form)
      }
      this.$emit('closeLoading')
    },
    // 根据商户查询商品
    searchProdsByCurrentShop (item, index) {
      console.log(item)
      if (Number.isInteger(index)) {
        this.$set(item, 'prodIds', [])
        this.$set(item, 'prodJson', null)
        this.$set(item, 'prodSpecifyIds', [])
        this.$set(item, 'prodSpecifyJson', null)
      }
      item.source = this.findShopDetailsByShopId(item.activityMerchantId)
      this.$http({
        url: this.$http.adornUrl('/prod/prod/gatCategoryProduct'),
        method: 'get',
        params: this.$http.adornParams({ shopId: item.activityMerchantId, dvyType: this.dvyType })
      }).then(({ data }) => {
        if (data) {
          if (this.acTypeInfo.id == 1 || this.acTypeInfo.id == 9) {
            this.$set(item, 'categoryProdListTreeA', deepCopy(data))
            this.$set(item, 'categoryProdListTreeB', deepCopy(data))
            this.handleCategoryProdChange(item.prodIds, 'AProdTree', index, item)
            this.handleCategoryProdChange(
              item.prodSpecifyIds,
              'BProdTree',
              index,
              item
            )
          } else {
            this.$set(item, 'categoryProdListTree', data)
          }
        }
      })
    },
    handleCategoryProdChange (e, refName, index) {
      console.log(e)
    },
    changeItemDiscountMethod (e, item) {
      console.log(e, item)
      if (e == 2) {
        this.$set(item, 'purchaseQuantity', '')
      } else {
        this.$set(item, 'discountProportion', '')
      }
    },
    findShopDetailsByShopId (shopId) {
      let shopDetails = this.selectShopList.find((f) => f.value == shopId)
      return shopDetails ? shopDetails.data : {}
    },
    delCombination (index) {
      this.$confirm('确定删除该组合吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(() => {
        this.form.activityMerchantProdList.splice(index, 1)
      })
    },
    validAcTypeForm () {
      console.log('this.form', this.form)
      return new Promise((resolve, reject) => {
        this.$refs.AcTypeForm.validate((valid) => {
          if (valid) {
            let list = deepCopy(this.form.activityMerchantProdList)
            this.form.shopDetailList = list.map(
              (f) => {
                if (f.prodIds && f.prodIds.length) {
                  f.prodJson = JSON.stringify({ prodIds: f.prodIds })
                }
                if (f.prodSpecifyIds && Array.isArray(f.prodSpecifyIds) && f.prodSpecifyIds.length) {
                  f.prodSpecifyJson = JSON.stringify({ prodSpecifyIds: f.prodSpecifyIds })
                }
                if (this.acTypeInfo.id == 2 || this.acTypeInfo.id == 7 || this.acTypeInfo.id == 11) {
                  f.prodSpecifyJson = JSON.stringify({ prodSpecifyIds: [f.prodSpecifyIds] })
                }
                f.activityMarketTypeId = this.acTypeInfo.id
                if (f.discountProportion !== null && f.discountProportion !== undefined) {
                  f.discountProportion = f.discountProportion / 100
                }
                if (this.acTypeInfo.id == 10) {
                  f.discountRate = f.discountRate / 100
                  f.specifyDiscountRate = f.specifyDiscountRate / 100
                }
                return f.source.shopDetail
              }
            )
            resolve({
              valid: true,
              data: {
                ...this.form,
                activityMerchantProdList: list
              }
            })
          } else {
            resolve({
              valid: false
            })
          }
        })
      })
    }
  }
}
export default mixins
